﻿using FI.Core.IoC;
using FI.Core.Properties;
using FI.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FI.Core.Configuration;
using FI.Core.SettingSource.Configuration;

namespace FI.Core.SettingSource
{
    /// <summary>
    /// A simple file based Setting Source.
    /// </summary>
    [MapTo(typeof(ISettingSource),0)]
    [ConfigurationElement(typeof(SimpleFileSettingSourceData))]
    public class SimpleFileSettingSource: ISettingSource
    {
        #region Properties
        /// <summary>
        /// Gets the configuration.
        /// </summary>
        /// <value>
        /// The configuration.
        /// </value>
        public System.Configuration.Configuration Configuration { get; private set; }

        /// <summary>
        /// Gets the configuration file path.
        /// </summary>
        /// <value>
        /// The configuration file path.
        /// </value>
        public string ConfigurationFilePath { get; private set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleFileSettingSource"/> class.
        /// </summary>
        public SimpleFileSettingSource(): this(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        { 
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleFileSettingSource"/> class.
        /// </summary>
        /// <param name="configurationFilePath">The configuration file path.</param>
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        public SimpleFileSettingSource(string configurationFilePath)
        {
            Guard.ArgumentNotNullOrEmpty(configurationFilePath, "configurationFilePath");
            string rootedFilePath = (Path.IsPathRooted(configurationFilePath) ? configurationFilePath : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configurationFilePath));
            if (!File.Exists(rootedFilePath))
            {
                throw new FileNotFoundException(Resources.ExceptionConfigurationLoadFileNotFound.Format((object)configurationFilePath));
            }
            this.ConfigurationFilePath = rootedFilePath;
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configurationFilePath };
            this.Configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the configuration section based on specified section name.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <returns>
        /// The target configuration section.
        /// </returns>
        public ConfigurationSection GetConfigurationSection(string sectionName)
        {
            Guard.ArgumentNotNullOrEmpty(sectionName, "sectionName");
            return this.Configuration.GetSection(sectionName);
        }
        #endregion

        /// <summary>
        /// Sets as current settings source.
        /// </summary>
        public void SetAsCurrentSettingsSource()
        {
            
        }
    }
}
